<?php


namespace app\common\taglib;
use think\template\TagLib;

class Tp extends TagLib
{
    protected $tags = array(
        'newscate'       => ['attr' => 'id,limit,articlecatename', 'close' => 1],
        'newslist'      => ['attr' => 'articleid,num,name,table,field', 'close' => 1],
        'pageart'      => ['attr' => 'articleid', 'close' => 1],
    );

    Public function tagNewscate($tag, $content){
        $tag['limit'] = $tag['limit'] ?? '0';
        $tag['id']    = $tag['id']    ?? '';
        $name         = $tag['articlecatename']  ?? 'Newscate';
        if (!empty($tag['id'])) {
            $cateStr = '$__CATE__ = \app\common\model\ArticleCate::where(\'is_menu\',1)->order(\'sort ASC,id DESC\')->select();';
            $cateStr .= '$__LIST__ = getChildsOn($__CATE__,' . $tag['id'] . ');';
        } else {
            $cateStr  = '$__CATE__ = \app\common\model\ArticleCate::where(\'is_menu\',1)->order(\'sort ASC,id DESC\')->select();';
            $cateStr .= '$__LIST__ = unlimitedForLayer($__CATE__);';
        }
        // 提取前N条数据,因为sql的LIMIT避免不了子栏目的问题
        if (!empty($tag['limit'])) {
            $cateStr .= '$__LIST__ = array_slice($__LIST__, 0,' . $tag['limit'] . ');';
        }
        $parse  = '<?php ';
        $parse .= $cateStr;
        $parse .= ' ?>';
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagNewslist($tag, $content) {
        //$table = $tag['table'];
        $field = $tag['field'];
        //首页cid=0,num>0 ; 分类页cid>0,num=0
        if(empty($tag['articleid']) || $tag['articleid']=='0'){
            $num = $tag['num'];
            $php = <<<php
<?php
            \$list = \app\common\model\Article::field('$field')->limit("$num")->order('id desc')->select();
            \$__LIST__ = \$list;
?>
php;
        }else{
            $num = 0;
            $where = "articleid=" . $tag['articleid'];
            $php = <<<php
<?php
           \$list = \app\common\model\Article::where("$where")->field('$field')->limit("$num")->order('id desc')->paginate(10);
           \$__LIST__ = \$list;
?>
php;
        }

        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagPageart($tag, $content) {
        //实际不使用，仅供教学参考
        $where = "articleid=" . $tag['articleid'];
        $php = <<<php
<?php
       \$article_list = \app\common\model\Article::where("$where")->field('id,articleid,article_name,image,description')->limit("$num")->order('id desc')->paginate(10);
       \$vo = \$article_list;
?>
php;
        $parse = $php;
        $parse .= $content;
        return $parse;
    }
}